import javafx.scene.layout.Background;

import java.security.spec.RSAOtherPrimeInfo;

public class PathFinderForTarget {

    static int [][]dir = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};

    static boolean dfs(int x, int y, char[][]matrix, int len, char []target, boolean [][]visited){
        if(len == target.length){
            return true;
        }

        if(matrix[x][y] == target[len]){
            len++;
            visited[x][y] = true;
            for(int i = 0; i < 4; i++){
                int newX = x + dir[i][0];
                int newY = y + dir[i][1];
                if(newX < 0 || newX >= matrix.length || newY < 0 || newY >= matrix[0].length){
                    continue;
                }
                if(visited[newX][newY]) continue;
                if(dfs(newX, newY, matrix, len, target,visited)){
                    return true;
                }
            }
            visited[x][y] = false;
            len--;
        }

        return false;
    }

    static boolean find(char[][]matrix, char []target, boolean [][]visited){
        for(int x = 0; x < matrix.length; x++){
            for(int y = 0; y < matrix[0].length; y++){
                if(dfs(x, y, matrix, 0, target, visited)){
                    return true;
                }
            }
        }
        return false;
    }

    public static void main(String []args){
        char [][]matrix = {
                {'a', 'b', 'c', 'd', 'e', 'f', 'g'},
                {'h', 'i', '3', 'k', 'l', 'm', 'n'},
                {'o', 'l', 'h', '1', 's', 't', 'u'},
                {'v', 'w', 'm', 'y', 'z', 'A', 'B'},
                {'C', 'D', 'E', 'F', 'G', 'H', 'I'},
                {'J', 'K', 'L', 'M', 'N', 'O', 'P'}
        };

        boolean [][]visited = new boolean[matrix.length][matrix[0].length];
        char []target = {'l', 'h', 'm'};
        if( find(matrix, target, visited) ){
            System.out.println("get");
        }else{
            System.out.println("not");
        }

    }

}
